// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Constraint/RBE2Parser.h"

#include "gtest/gtest.h"

#include "DataStructure/Common/Validation.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/IRBE2Service.h"

#include "Import/BDFImport/BDFParserBase.h"
using namespace SG::DBManager;
using namespace SG::DataStructure::FEM;
using namespace SG::Import;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;

TEST (RBE2ParserTest, ParseEntry1)
{
    DB_SERVICEFACTORY->ClearDatabase ();
    std::vector<std::string> Entry = { "RBE2     8121    417     123456  1626" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseRBE2 (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::RBE2Data data;

    DB_SERVICEFACTORY->get<IRBE2Service> ()->FindByIndex (0, data);

    ASSERT_TRUE (data.m_alpha == 0.0);
    ASSERT_TRUE (data.m_id == 8121);
    ASSERT_TRUE (data.GetSlaveDofs ().size () == 6);
    ASSERT_TRUE (data.m_slaveNodes.size () == 1);
    ASSERT_TRUE (data.m_slaveNodes[0] == 1626);
    ASSERT_TRUE (data.m_masterNode == 417);
}

TEST (RBE2ParserTest, ParseEntry2)
{
    DB_SERVICEFACTORY->ClearDatabase ();
    std::vector<std::string> Entry = { "RBE2     633604  632257  123456  632207  632307    2.2-5" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseRBE2 (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::RBE2Data data;

    DB_SERVICEFACTORY->get<IRBE2Service> ()->FindById (633604, data);

    ASSERT_TRUE (SG::DataStructure::Common::isNear (data.m_alpha, 2.2e-5));
    ASSERT_TRUE (data.m_id == 633604);
    ASSERT_TRUE (data.GetSlaveDofs ().size () == 6);
    ASSERT_TRUE (data.m_slaveNodes.size () == 2);
    ASSERT_TRUE (data.m_slaveNodes[0] == 632207);
    ASSERT_TRUE (data.m_masterNode == 632257);
}
